/************************************
GET FACET TERMS IN RESULTS PAGE
Written by Ethan Gruber, gruber@numismatics.org
Library: jQuery
Description: This utilizes ajax to populate the list of terms in the facet category in the results page.  
If the list is populated and then hidden, when it is re-activated, it fades in rather than executing the ajax call again.
************************************/
$(document).ready(function() {
	var popupStatus = 0;

	$('.facet_label').click(function () {
		var list_id = $(this) .attr('id').split('_link')[0] + '-list';
		var category = $(this) .attr('id') .split('_link')[0];
		var q = $(this) .attr('label');
		  if ($(this) .children('.get_facets') .attr('src').indexOf('plus') > 0) {
			if ($('#' + list_id).html().indexOf('<li>') < 0) {
				$.get('get_facets', {
						q: q, category: category, sort: 'count', limit:50, offset:0
					},
					function (data) {
						$('#' + list_id) .html(data);
					});			
			} 
			$(this) .children('.get_facets') .attr('src', 'images/minus.png');
			$('#' + list_id) .fadeIn('slow');	
			if (popupStatus == 0) {				
				$("#backgroundPopup").fadeIn("fast");
				popupStatus = 1;
			}			
		} else if ($(this) .children('.get_facets') .attr('src').indexOf('minus') > 0) {
			$(this) .children('.get_facets') .attr('src', 'images/plus.png');
			$('#' + list_id) .fadeOut('fast');
		}
	});
	$("#backgroundPopup").livequery('click', function(event) {
		disablePopup();
	});
	
	$('.sort_facet') .livequery('click', function(event) {
		var category = $(this) .attr('id') .split('-')[0];
		var list_id = category + '-list';		
		var q = $(this) .attr('label');
		var sort = $(this) .attr('id') .split('-')[1];
		$('#' + list_id) .attr('class', 'facet_list facet_list_expanded');
		$.get('get_facets', {
			q: q, category: category, sort: sort, limit: 50, offset: 0
		},
			function (data) { 
				$('#' + list_id) .html(data);
		});
		if (popupStatus == 0) {				
			$("#backgroundPopup").fadeIn("fast");
			popupStatus = 1;
		}
	});
	
	$('.page-facets').livequery('click', function(event){
		var category = $(this) .attr('id') .split('-')[0];
		var list_id = category + '-list';	
		var q = $(this) .attr('label');
		var offset = $(this) .attr('title').split(':')[0];
		var sort = $(this) .attr('title').split(':')[1];
		$.get('get_facets', {
			q: q, category: category, sort: sort, limit: 50, offset: offset
		},
			function (data) { 
				$('#' + list_id) .html(data);
		});
	});
	
	$('.close_facets').livequery('click', function(event){
		disablePopup();
	});
	
	/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;

//disabling popup with jQuery magic!
	function disablePopup() {
	//disables popup only if it is enabled
		if (popupStatus == 1) {	
			$("#backgroundPopup").fadeOut("fast");
			$('.facet_list') .fadeOut('fast');
			$('.facet_label') .children('.get_facets') .attr('src', 'images/plus.png');
			popupStatus = 0;		
		}
	}
});

